package com.ymt.bpm.engine.listener;

import com.ymt.bpm.appboot.SpringContext;
import com.ymt.bpm.engine.EngineConst;
import com.ymt.bpm.engine.listener.engine.exception.EngineException;
import com.ymt.bpm.engine.listener.engine.exception.ErrorCode;
import com.ymt.bpm.engine.handler.process.ProcessExcutionHandler;
import com.ymt.bpm.service.platform.log.SysLogService;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Johnny
 * Process Execution Listener added on model
 *
 */
public class ProcessExecutionListener implements ExecutionListener {

	/**
	 * auto generated serialVersionUID
	 */
	private static final long serialVersionUID = -348403396911624028L;
	/**
	 * Logger
	 */
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	/**
	 * ProcessExcutionHandler
	 */
	@Autowired
	private ProcessExcutionHandler handler;

	public ProcessExecutionListener() {
		handler = SpringContext.getBean(ProcessExcutionHandler.class);
	}


	/**
	 * 执行监听任务
	 */
	@Override
	@Transactional(EngineConst.TRANSACTION_MANAGER)
	public void notify(DelegateExecution execution) throws Exception {

		if (logger.isDebugEnabled()) {
			logger.debug("execution.getEventName()=="+execution.getEventName());
			logger.debug("execution.getParentId()=="+execution.getParentId());
			logger.debug("execution.getProcessInstanceId()=="+execution.getProcessInstanceId());
			logger.debug("execution.getId()=="+execution.getId());
			logger.debug("execution.getVariable(processVar)=="+execution.getVariable("processVar"));
			logger.debug("execution.getVariable(parentBpdId)=="+execution.getVariable("parentBpdId"));
			logger.debug("execution.getVariable(parentPiEid)=="+execution.getVariable("parentPiEid"));
			logger.debug("execution.getVariable(isCallActivity)=="+execution.getVariable("isCallActivity"));
		}

		try {
			if (EVENTNAME_START.equals(execution.getEventName())) {
				//监听流程实例start
				handler.onProcessStart(execution);
			} else if (EVENTNAME_END.equals(execution.getEventName())) {
				handler.onProcessComplete(execution);
			} else if (EVENTNAME_TAKE.equals(execution.getEventName())) {
				handler.onProcessTake(execution);
			}
		} catch (Exception e) {
			e.printStackTrace();
			// 记录错误日志
			try {
				SysLogService logService = SpringContext.getBean(SysLogService.class);
				logService.expLog(execution.getTenantId(),
						execution.getProcessInstanceId(),
						execution.getCurrentActivityId(),
						"",
						"0",
						null, null, e);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			throw new RuntimeException(
					new EngineException(ErrorCode.PI_FAIL,
					"Process instance failed on event:" + execution.getEventName()+", executionId="+execution.getId(),
					e));//for rollback
		}

	}

}
